"""
汽车之家数据分析系统 - 美观版
现代化设计的主程序入口
"""

import sys
import os
from PyQt5.QtWidgets import QApplication, QSplashScreen, QMessageBox, QLabel
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPixmap, QFont, QPainter, QColor, QLinearGradient
from ui.main_window import BeautifulMainWindow


def create_beautiful_splash():
    """创建美观的启动画面"""
    # 创建启动画面
    splash_pixmap = QPixmap(500, 350)

    # 创建渐变背景
    painter = QPainter(splash_pixmap)

    # 设置渐变背景
    gradient = QLinearGradient(0, 0, 500, 350)
    gradient.setColorAt(0, QColor(102, 126, 234))  # #667eea
    gradient.setColorAt(1, QColor(118, 75, 162))  # #764ba2

    painter.fillRect(splash_pixmap.rect(), gradient)

    # 绘制标题
    painter.setPen(QColor(255, 255, 255))
    painter.setFont(QFont("Microsoft YaHei", 20, QFont.Bold))

    title_rect = splash_pixmap.rect()
    title_rect.setTop(100)
    title_rect.setHeight(60)
    painter.drawText(title_rect, Qt.AlignCenter, "🚗 汽车之家数据分析系统")

    # 绘制副标题
    painter.setFont(QFont("Microsoft YaHei", 12))
    subtitle_rect = splash_pixmap.rect()
    subtitle_rect.setTop(160)
    subtitle_rect.setHeight(30)
    painter.drawText(subtitle_rect, Qt.AlignCenter, "智能汽车数据爬取与可视化分析平台")

    # 绘制版本信息
    painter.setFont(QFont("Microsoft YaHei", 10))
    version_rect = splash_pixmap.rect()
    version_rect.setTop(200)
    version_rect.setHeight(30)
    painter.drawText(version_rect, Qt.AlignCenter, "Version 2.0 - 美观版")

    # 绘制加载信息
    painter.setFont(QFont("Microsoft YaHei", 9))
    loading_rect = splash_pixmap.rect()
    loading_rect.setTop(280)
    loading_rect.setHeight(30)
    painter.drawText(loading_rect, Qt.AlignCenter, "正在启动应用程序...")

    painter.end()

    splash = QSplashScreen(splash_pixmap)
    splash.setStyleSheet("""
        QSplashScreen {
            border: 3px solid rgba(255, 255, 255, 0.3);
            border-radius: 15px;
        }
    """)

    return splash


def setup_application_style():
    """设置应用程序全局样式"""
    return """
        QApplication {
            font-family: "Microsoft YaHei UI", "Segoe UI", sans-serif;
        }

        QWidget {
            font-family: "Microsoft YaHei UI", "Segoe UI", sans-serif;
        }

        QMessageBox {
            background-color: white;
            border-radius: 10px;
        }

        QMessageBox QLabel {
            color: #2c3e50;
            font-size: 12px;
        }

        QMessageBox QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #4facfe, stop:1 #00f2fe);
            color: white;
            border: none;
            border-radius: 6px;
            padding: 8px 16px;
            font-weight: bold;
            min-width: 80px;
        }

        QMessageBox QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #43a3f5, stop:1 #00d4e6);
        }

        QFileDialog {
            background-color: white;
        }

        QFileDialog QListView {
            background-color: white;
            border: 1px solid #e0e6ed;
            border-radius: 4px;
        }

        QFileDialog QTreeView {
            background-color: white;
            border: 1px solid #e0e6ed;
            border-radius: 4px;
        }
    """


def check_system_requirements():
    """检查系统要求"""
    missing_deps = []

    try:
        import requests
    except ImportError:
        missing_deps.append("requests")

    try:
        from PyQt5.QtWidgets import QApplication
    except ImportError:
        missing_deps.append("PyQt5")

    return missing_deps


def show_welcome_message(parent):
    """显示欢迎消息"""
    welcome_text = """
    <div style='text-align: center; padding: 20px;'>
    <h2>🎉 欢迎使用汽车数据分析系统！</h2>
    <br>
    <p style='font-size: 14px; color: #34495e;'>
    这是一个现代化的汽车数据爬取与分析平台<br>
    具有美观的界面设计和强大的数据处理能力
    </p>
    <br>
    <p style='font-size: 12px; color: #7f8c8d;'>
    <b>✨ 主要功能:</b><br>
    🔍 智能数据爬取 | 📊 可视化展示 | 📈 统计分析<br>
    🔎 快速搜索 | 📋 数据导出 | 🎨 现代化UI
    </p>
    <br>
    <p style='font-size: 12px; color: #95a5a6;'>
    点击 "🚀 获取数据" 按钮开始体验
    </p>
    </div>
    """

    msg = QMessageBox(parent)
    msg.setWindowTitle("🎉 欢迎")
    msg.setText(welcome_text)
    msg.setStyleSheet("""
        QMessageBox {
            background-color: white;
            border-radius: 12px;
            min-width: 400px;
            min-height: 300px;
        }
    """)
    msg.exec_()


def main():
    """主函数"""
    # 创建应用程序
    app = QApplication(sys.argv)
    app.setApplicationName("汽车之家数据分析系统")
    app.setApplicationVersion("2.0 美观版")
    app.setOrganizationName("智能数据分析")

    # 设置应用程序字体
    font = QFont("Microsoft YaHei UI", 9)
    app.setFont(font)

    # 设置全局样式
    app.setStyleSheet(setup_application_style())

    # 检查系统要求
    missing_deps = check_system_requirements()
    if missing_deps:
        QMessageBox.critical(None, "❌ 系统要求不满足",
                             f"缺少以下依赖项:\n{', '.join(missing_deps)}\n\n"
                             f"请使用以下命令安装:\npip install {' '.join(missing_deps)}")
        sys.exit(1)

    # 创建并显示启动画面
    splash = create_beautiful_splash()
    splash.show()

    # 处理启动画面显示
    app.processEvents()

    try:
        # 模拟加载过程
        splash.showMessage("🔧 正在初始化组件...", Qt.AlignCenter | Qt.AlignBottom, Qt.white)
        app.processEvents()
        QTimer.singleShot(800, lambda: None)  # 延迟800ms
        app.processEvents()

        splash.showMessage("🎨 正在加载美观界面...", Qt.AlignCenter | Qt.AlignBottom, Qt.white)
        app.processEvents()
        QTimer.singleShot(600, lambda: None)  # 延迟600ms
        app.processEvents()

        splash.showMessage("🚀 准备启动应用程序...", Qt.AlignCenter | Qt.AlignBottom, Qt.white)
        app.processEvents()

        # 创建主窗口
        main_window = BeautifulMainWindow()

        # 延迟显示主窗口和欢迎消息
        def show_main_window():
            splash.finish(main_window)
            main_window.show()

            # 延迟显示欢迎消息
            QTimer.singleShot(500, lambda: show_welcome_message(main_window))

        QTimer.singleShot(1200, show_main_window)  # 1.2秒后显示主窗口

        # 运行应用程序
        sys.exit(app.exec_())

    except Exception as e:
        splash.close()
        QMessageBox.critical(None, "❌ 启动失败",
                             f"应用程序启动失败:\n{str(e)}\n\n"
                             f"请检查系统环境和依赖项是否正确安装。")
        print(f"应用程序启动失败: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()